﻿using System;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace App
{
    class CTabsDesigner : ParentControlDesigner
    {
        IComponentChangeService changeService;

        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            //Design services
            changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

            //Bind design events
            changeService.ComponentRemoving += new ComponentEventHandler(OnRemoving);

            Verbs.Add(new DesignerVerb("添加选项卡", new EventHandler(OnAddTabStrip)));
            Verbs.Add(new DesignerVerb("删除选项卡", new EventHandler(OnRemoveTabStrip)));
        }

        protected override void Dispose(bool disposing)
        {
            changeService.ComponentRemoving -= new ComponentEventHandler(OnRemoving);

            base.Dispose(disposing);
        }

        private void OnRemoving(object sender, ComponentEventArgs e)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));

            //Removing a button
            if (e.Component is CTab)
            {
                CTab itm = e.Component as CTab;
                if (Control.TabPages.Contains(itm))
                {
                    changeService.OnComponentChanging(Control, null);
                    Control.TabPages.Remove(itm);
                    changeService.OnComponentChanged(Control, null, null, null);
                    return;
                }
            }

            if (e.Component is CTab)
            {
                for (int i = Control.TabPages.Count - 1; i >= 0; i--)
                {
                    CTab itm = Control.TabPages[i] as CTab;
                    changeService.OnComponentChanging(Control, null);
                    Control.TabPages.Remove(itm);
                    host.DestroyComponent(itm);
                    changeService.OnComponentChanged(Control, null, null, null);
                }
            }
        }

        private void OnAddTabStrip(object sender, EventArgs e)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerTransaction transaction = host.CreateTransaction("添加选项卡");
            CTab itm = (CTab)host.CreateComponent(typeof(CTab));
            changeService.OnComponentChanging(Control, null);
            Control.TabPages.Add(itm);
            int indx = Control.TabPages.IndexOf(itm) + 1;
            itm.Text = "TabStrip Page " + indx.ToString();
            Control.SelectedTab = itm;
            changeService.OnComponentChanged(Control, null, null, null);
            transaction.Commit();
        }

        private void OnRemoveTabStrip(object sender, EventArgs e)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerTransaction transaction = host.CreateTransaction("删除选项卡");
            changeService.OnComponentChanging(Control, null);
            CTab itm = Control.TabPages[Control.TabPages.Count - 1] as CTab;
            Control.TabPages.Remove(itm);
            changeService.OnComponentChanged(Control, null, null, null);
            transaction.Commit();
        }


        public new virtual CTabs Control
        {
            get
            {
                return base.Control as CTabs;
            }
        }

        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == 0x201)
            {
                Point pt = Control.PointToClient(Cursor.Position);
                pt = Cursor.Position;
                for (int i = 0; i < Control.TabPages.Count; i++)
                {
                    if (Control.GetTabRect(i).Contains(Cursor.Position))
                    {
                        Control.SelectedTab = Control.TabPages[i];
                        ArrayList selection = new ArrayList();
                        selection.Add(Control.TabPages[i]);
                        ISelectionService selectionService = (ISelectionService)GetService(typeof(ISelectionService));
                        selectionService.SetSelectedComponents(selection);
                        break;
                    }
                }
            }


            base.WndProc(ref msg);
        }

    }
}
